package model;

import java.util.ArrayList;
import java.util.Arrays;

public class EnergyProfile {
	public static final String[] DAY_TYPE = {"Average Weekday","Average Weekend"};
	public static final int WEEKDAY = 0;
	public static final int WEEKEND = 1;
	
	public static final String[] MONTH = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
	
	public static final String[] WRITE_HEADER = {"UsageType","Industry","Month","Daytype",
		"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"};
	
	public static final String[] INDUSTRY = {"Commercial","Industrial","Residential"};
	public static final int COMMERCIAL = 0;
	public static final int INDUSTRIAL = 1;
	public static final int RESIDENTIAL = 2;
	
	private String usageType;
	private String industry;
	
	private double[][] monthWeekDayProfile;
	private double[][] monthWeekEndProfile;
	
	public EnergyProfile() {
		usageType = null;
		monthWeekDayProfile = new double[12][24];//new HashMap();
		monthWeekEndProfile = new double[12][24];// new HashMap();
	}
	
	public static EnergyProfile addEnergyProfiles(EnergyProfile ep1, EnergyProfile ep2) {
		EnergyProfile newep = new EnergyProfile();
		newep.setUsageType(ep1.getUsageType()+"&"+ep2.getUsageType());
		newep.setIndustry(ep1.getIndustry());
		//add by months for each day profile
		
		for(int month = 0; month<12; month++) {
			double[] ep1dayarr = ep1.monthWeekDayProfile[month];
			double[] ep2dayarr = ep2.monthWeekDayProfile[month];
			double[] ep1endarr = ep1.monthWeekEndProfile[month];
			double[] ep2endarr = ep2.monthWeekEndProfile[month];
			
			double[] newepday = new double[24];
			double[] newepend = new double[24];
			for (int i=0; i<24; i++) {
				newepday[i] = ep1dayarr[i]+ep2dayarr[i];
				newepend[i] = ep1endarr[i]+ep2endarr[i];
			}
			/*newep.monthWeekDayProfile.put(strmonth,newepday);
			newep.monthWeekEndProfile.put(strmonth,newepend);*/
			newep.monthWeekDayProfile[month] = newepday;
			newep.monthWeekEndProfile[month] = newepend;
		}
		return newep;
	}
	
	public ArrayList<String[]> getRecords(){
		ArrayList<String[]> list = new ArrayList<String[]>();
		for (int month=0; month<12; month++) {
			String[] str = new String[WRITE_HEADER.length];
			str[0] = this.getUsageType();
			str[1] = this.getIndustry();
			str[2] = String.valueOf(month+1);
			str[3] = "0"; //DAY_TYPE_WEEKDAY;
			
			for (int i=0; i<24; i++) {
				str[4+i] = String.valueOf(monthWeekDayProfile[month][i]);
			}
			list.add(str);
		}
		
		for (int month=0; month<12; month++) {
			String[] str = new String[WRITE_HEADER.length];
			str[0] = this.getUsageType();
			str[1] = this.getIndustry();
			str[2] = String.valueOf(month+1);
			str[3] = "1"; //DAY_TYPE_WEEKEND;
			
			for (int i=0; i<24; i++) {
				str[4+i] = String.valueOf(monthWeekEndProfile[month][i]);
			}
			list.add(str);
		}		
		return list;		
	}
	
	public String[] getMonthDayRecord(int month, String daytype) {
		String[] str = new String[WRITE_HEADER.length];
		str[0] = this.getUsageType();
		str[1] = this.getIndustry();
		str[2] = daytype;
		double[] dayprofile = getMonthDayProfile(month,daytype);
		for (int i=0; i<24; i++) {
			str[3+i] = String.valueOf(dayprofile[i]);
		}
		
		return str;
	}
	

	
	/**
	 * divide profile values by maximum
	 * 
   1. int[] nums={6,-1,-2,-3,0,1,2,3,4};  
   2. Arrays.sort(nums);  
   3. System.out.println("Minimum = " + nums[0]);  
   4. System.out.println("Maximum = " + nums[nums.length-1]);  
	 */
	public void normalizeData() {
		double[] daytemp = new double[24];
		double[] endtemp = new double[24];
		for (int month=0; month<12; month++) {
			daytemp = Arrays.copyOf(monthWeekDayProfile[month],24);
			endtemp = Arrays.copyOf(monthWeekEndProfile[month],24);
			Arrays.sort(daytemp);
			Arrays.sort(endtemp);
			
			for (int i=0; i<24; i++) {
				monthWeekDayProfile[month][i] = monthWeekDayProfile[month][i]/daytemp[daytemp.length-1];
				monthWeekEndProfile[month][i] = monthWeekEndProfile[month][i]/endtemp[endtemp.length-1];
			}
		}
	}
	
	public double[] getMonthDayProfile(int month, String daytype) {
		double[] daytemp = new double[24];
		if (EnergyProfile.DAY_TYPE[EnergyProfile.WEEKDAY].equals(daytype))
			daytemp = monthWeekDayProfile[month];
		else 
			daytemp = monthWeekEndProfile[month];
	
		return daytemp;
	}
	

	
	public String toString() {
		String weekday="",weekend="";
		for (int i=0;i<12;i++)
			for(int j=0;j<24;j++) {
				weekday = weekday.concat(String.valueOf(monthWeekDayProfile[i][j])) +" ";
				weekend = weekend.concat(String.valueOf(monthWeekEndProfile[i][j])) +" ";
			}
			
		return "Industry: " + getIndustry() + " UsageType: "+getUsageType() +"\n"
			+"Weekday: "+weekday+"\n"
			+"Weekend: "+weekend+"\n";
	}
	
	public String getUsageType() {
		return usageType;
	}

	public void setUsageType(String usageType) {
		this.usageType = usageType;
	}

	public String getIndustry() {
		return industry;
	}

	public void setIndustry(String industry) {
		this.industry = industry;
	}

	public double[][] getMonthWeekDayProfile() {
		return monthWeekDayProfile;
	}

	public void setMonthWeekDayProfile(double[][] monthWeekDayProfile) {
		this.monthWeekDayProfile = monthWeekDayProfile;
	}

	public double[][] getMonthWeekEndProfile() {
		return monthWeekEndProfile;
	}

	public void setMonthWeekEndProfile(double[][] monthWeekEndProfile) {
		this.monthWeekEndProfile = monthWeekEndProfile;
	}
}
